package cn.zl.model;

import cn.zl.GB28181.core.SipTransactionInfo;
import cn.yunbamboos.Model;
import cn.yunbamboos.mybatis.annotation.TableField;
import cn.yunbamboos.mybatis.annotation.TableName;
import cn.yunbamboos.mybatis.annotation.TablePrimaryKey;
import cn.yunbamboos.rest.service.anno.param.ParamType;
import cn.yunbamboos.rest.service.anno.param.enums.Type;
import com.alibaba.fastjson2.JSONObject;

@TableName(value = "zl_device", cn = "国标设备表")
public class Device implements Model {

    @ParamType(path = "device_id", type = Type.String, title = "国标设备编号", required = true, example = "")
    @TablePrimaryKey(value = "device_id")
    private String deviceId;
    @ParamType(path = "device_name", type = Type.String, title = "国标设备名称", required = true, example = "")
    @TableField(value = "device_name")
    private String deviceName;
    @ParamType(path = "password", type = Type.String, title = "密码", required = true, example = "")
    @TableField(value = "password")
    private String password;
    @ParamType(path = "manufacturer", type = Type.String, title = "厂家", required = true, example = "")
    @TableField(value = "manufacturer")
    private String manufacturer;
    @TableField(value = "model")
    private String model;
    @TableField(value = "firmware")
    private String firmware;
    /**
     * 信令传输模式
     * */
    @ParamType(path = "transport", type = Type.String, title = "信令传输模式", required = true, example = "TCP")
    @TableField(value = "transport")
    private String transport;
    /**
     * 流传输模式
     * UDP UDP模式
     * TCP-ACTIVE TCP主动模式
     * TCP-PASSIVE TCP被动模式
     * */
    @ParamType(path = "stream_mode", type = Type.String, title = "流传输模式", required = true, example = "TCP-ACTIVE")
    @TableField(value = "stream_mode")
    private String streamMode;
    @TableField(value = "expires")
    private int expires;
    @TableField(value = "ip")
    private String ip;
    @TableField(value = "port")
    private int port;
    @TableField(value = "host_address")
    private String hostAddress;
    @TableField(value = "local_ip")
    private String localIp;
    @TableField(value = "charset")
    private String charset;
    @TableField(value = "keepalive_interval_time")
    private int keepaliveIntervalTime;
    @ParamType(path = "on_line", type = Type.Boolean, title = "状态", required = true, example = "false", description = "false 离线,true 在线")
    @TableField(value = "on_line")
    private boolean onLine;
    @ParamType(path = "register_time", type = Type.String, title = "最近注册时间", required = true, example = "2024-01-01 00:00:00")
    @TableField(value = "register_time")
    private String registerTime;
    @ParamType(path = "keepalive_time", type = Type.String, title = "最近心跳时间", required = true, example = "2024-01-01 00:00:00")
    @TableField(value = "keepalive_time")
    private String keepaliveTime;
    /**
     * 目录订阅周期，0为不订阅
     */
    @ParamType(path = "subscribe_cycle_for_catalog", type = Type.String, title = "目录订阅周期", required = false, example = "2024-01-01 00:00:00", description = "目录订阅周期，o为不订阅")
    @TableField(value = "subscribe_cycle_for_catalog")
    private int subscribeCycleForCatalog = 0;
    @ParamType(path = "sdp_ip", type = Type.String, title = "收流IP", example = "127.0.0.1")
    @TableField(value = "sdp_ip")
    private String sdpIp;
    @ParamType(path = "ssrc_check", type = Type.String, title = "SSRC校验", example = "false")
    @TableField(value = "ssrc_check")
    private boolean ssrcCheck;
    @ParamType(path = "update_time", type = Type.String, title = "更新时间", required = true, example = "2024-01-01 00:00:00")
    @TableField(value = "update_time")
    private String updateTime;
    @ParamType(path = "create_time", type = Type.String, title = "创建时间", required = true, example = "2024-01-01 00:00:00")
    @TableField(value = "create_time")
    private String createTime;
    @ParamType(path = "channel_count", type = Type.Integer, title = "通道数", required = true, example = "0")
    private int channelCount;

    private SipTransactionInfo sipTransactionInfo;

    @Override
    public JSONObject encode() {
        JSONObject json = new JSONObject();
        json.put("device_id", deviceId);
        json.put("device_name", deviceName);
        json.put("password", password);
        json.put("manufacturer", manufacturer);
        json.put("model", model);
        json.put("firmware", firmware);
        json.put("transport", transport);
        json.put("stream_mode", streamMode);
        json.put("expires", expires);
        json.put("ip", ip);
        json.put("port", port);
        json.put("host_address", hostAddress);
        json.put("local_ip", localIp);
        json.put("charset", charset);
        json.put("keepalive_interval_time", keepaliveIntervalTime);
        json.put("on_line", onLine);
        json.put("register_time", registerTime);
        json.put("keepalive_time", keepaliveTime);
        json.put("subscribe_cycle_for_catalog", subscribeCycleForCatalog);
        json.put("sdp_ip", sdpIp);
        json.put("ssrc_check", ssrcCheck);
        json.put("update_time", updateTime);
        json.put("create_time", createTime);
        json.put("channel_count", channelCount);
        return json;
    }

    @Override
    public void decode(JSONObject json) {
        if (json.containsKey("device_id")) this.deviceId = json.getString("device_id");
        if (json.containsKey("device_name")) this.deviceName = json.getString("device_name");
        if (json.containsKey("password")) this.password = json.getString("password");
        if (json.containsKey("manufacturer")) this.manufacturer = json.getString("manufacturer");
        if (json.containsKey("model")) this.model = json.getString("model");
        if (json.containsKey("firmware")) this.firmware = json.getString("firmware");
        if (json.containsKey("transport")) this.transport = json.getString("transport");
        if (json.containsKey("stream_mode")) this.streamMode = json.getString("stream_mode");
        if (json.containsKey("expires")) this.expires = json.getInteger("expires");
        if (json.containsKey("ip")) this.ip = json.getString("ip");
        if (json.containsKey("port")) this.port = json.getInteger("port");
        if (json.containsKey("host_address")) this.hostAddress = json.getString("host_address");
        if (json.containsKey("local_ip")) this.localIp = json.getString("local_ip");
        if (json.containsKey("charset")) this.charset = json.getString("charset");
        if (json.containsKey("keepalive_interval_time")) this.keepaliveIntervalTime = json.getInteger("keepalive_interval_time");
        if (json.containsKey("on_line")) this.onLine = json.getBoolean("on_line");
        if (json.containsKey("subscribe_cycle_for_catalog")) this.subscribeCycleForCatalog = json.getInteger("subscribe_cycle_for_catalog");
        if (json.containsKey("sdp_ip")) this.sdpIp = json.getString("sdp_ip");
        if (json.containsKey("ssrc_check")) this.ssrcCheck = json.getBoolean("ssrc_check");
        if (json.containsKey("update_time")) this.updateTime = json.getString("update_time");
        if (json.containsKey("create_time")) this.createTime = json.getString("create_time");
    }

    public Integer getStreamModeForParam() {
        if (streamMode == null) {
            return 0;
        }
        if (streamMode.equalsIgnoreCase("UDP")) {
            return 0;
        }else if (streamMode.equalsIgnoreCase("TCP-PASSIVE")) {
            return 1;
        }else if (streamMode.equalsIgnoreCase("TCP-ACTIVE")) {
            return 2;
        }
        return 0;
    }

    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }

    public String getDeviceName() {
        return deviceName;
    }

    public void setDeviceName(String deviceName) {
        this.deviceName = deviceName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getFirmware() {
        return firmware;
    }

    public void setFirmware(String firmware) {
        this.firmware = firmware;
    }

    public String getTransport() {
        return transport;
    }

    public void setTransport(String transport) {
        this.transport = transport;
    }

    public String getStreamMode() {
        return streamMode;
    }

    public void setStreamMode(String streamMode) {
        this.streamMode = streamMode;
    }

    public int getExpires() {
        return expires;
    }

    public void setExpires(int expires) {
        this.expires = expires;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getHostAddress() {
        return hostAddress;
    }

    public void setHostAddress(String hostAddress) {
        this.hostAddress = hostAddress;
    }

    public String getLocalIp() {
        return localIp;
    }

    public void setLocalIp(String localIp) {
        this.localIp = localIp;
    }

    public String getCharset() {
        return charset;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public int getKeepaliveIntervalTime() {
        return keepaliveIntervalTime;
    }

    public void setKeepaliveIntervalTime(int keepaliveIntervalTime) {
        this.keepaliveIntervalTime = keepaliveIntervalTime;
    }

    public int getSubscribeCycleForCatalog() {
        return subscribeCycleForCatalog;
    }

    public void setSubscribeCycleForCatalog(int subscribeCycleForCatalog) {
        this.subscribeCycleForCatalog = subscribeCycleForCatalog;
    }

    public boolean isOnLine() {
        return onLine;
    }

    public void setOnLine(boolean onLine) {
        this.onLine = onLine;
    }

    public String getRegisterTime() {
        return registerTime;
    }

    public void setRegisterTime(String registerTime) {
        this.registerTime = registerTime;
    }

    public String getKeepaliveTime() {
        return keepaliveTime;
    }

    public void setKeepaliveTime(String keepaliveTime) {
        this.keepaliveTime = keepaliveTime;
    }

    public String getSdpIp() {
        return sdpIp;
    }

    public void setSdpIp(String sdpIp) {
        this.sdpIp = sdpIp;
    }

    public boolean isSsrcCheck() {
        return ssrcCheck;
    }

    public void setSsrcCheck(boolean ssrcCheck) {
        this.ssrcCheck = ssrcCheck;
    }

    public String getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(String updateTime) {
        this.updateTime = updateTime;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public int getChannelCount() {
        return channelCount;
    }

    public void setChannelCount(int channelCount) {
        this.channelCount = channelCount;
    }

    public SipTransactionInfo getSipTransactionInfo() {
        return sipTransactionInfo;
    }

    public void setSipTransactionInfo(SipTransactionInfo sipTransactionInfo) {
        this.sipTransactionInfo = sipTransactionInfo;
    }
}
